# == Schema Information
#
# Table name: wikis
#
#  id               :integer          not null, primary key
#  title            :string
#  content          :text
#  publish_time     :datetime
#  source           :string
#  author           :string
#  visited          :integer
#  liked            :integer
#  created_at       :datetime         not null
#  updated_at       :datetime         not null
#  wiki_category_id :integer
#  first_img        :string
#  summary          :text
#  deleted_at       :datetime
#

class Wiki < ApplicationRecord
  include ActsAsParanoidConcern

  acts_as_taggable

  validates :title, presence: { message: '标题不能为空' }
  validates :content, presence: { message: '正文不能为空' }
  validates :wiki_category_id, presence: { message: '分类不能为空' }

  attr_accessor :fake_tag_list

  before_create :set_publish_time

  before_save :contains_video?

  class << self

    def get_lastest_video_wiki
      Wiki.where(contains_video: true).order("created_at DESC").limit(1).first
    end
  end

  private

  def set_publish_time
    self.publish_time = Time.now
  end

  def contains_video?
    doc = Nokogiri::HTML(self.content, nil, 'utf-8')
    if !!doc.at('embed')
      self.contains_video = true
    end
  end

end
